<?php

class AdminView extends AuthView
{
	public function __construct(AdminModel $model)
	{
		parent::__construct($model);
	}
	
	protected function title() { return TextManager::i()->text("PAGE_TITLE_ADMIN", false); }
	protected function name() { return "admin"; }
	
	protected function content()
	{
		if (!$this->model()->hasError())
		{
			if (Input::request("action") == "conf_remove")
				$this->confirm();
		}
	?>
	<br /><br />
	<?php
		$this->viewPlayersList();
	}
	
	private function viewPlayersList()
	{
		$errors = $this->model()->errors();
		$playerList = $this->model()->playerList();
		
		$acLevels = array
		(
			Access::PLAYER => "AC_LEVEL_PLAYER",
			Access::ADMIN => "AC_LEVEL_ADMIN",
		);
		
		$actions = array
		(
			array("action" => "conf_remove", "name" => "ACTION_NAME_DELETE"),
		);
	?>
	<table border = "1">
		<tr>
			<th><?php TextManager::tr("COL_NAME"); ?></th>
			<th><?php TextManager::tr("COL_EMAIL"); ?></th>
			<th><?php TextManager::tr("COL_AC_LEVEL"); ?></th>
			<th><?php TextManager::tr("COL_ACTION"); ?></th>
			<th><?php TextManager::tr("COL_CHANGE_PASS"); ?></th>
		</tr>
		<?php foreach ($playerList as $player): ?>
		<tr>
			<td><?php echo $player->name(); ?></td>
			<td><?php echo $player->mail(); ?></td>
			<td>
				<form action = "<?php $this->url(array("action" => "ch_ac_level", "id" => $player->id())); ?>" method = "POST">
					<select name = "ac_level" onchange="this.form.submit();">
						<?php foreach ($acLevels as $key => $val): ?>
							<option value="<?php echo $key; ?>"<?php if ($key == $player->acLevel()): ?> selected<?php endif; ?>><?php TextManager::tr($val); ?></option>
						<?php endforeach; ?>
					</select>
				</form>
			</td>
			<td>
				<?php foreach ($actions as $action): ?>
					<a href = "<?php $this->url(array("action" => $action["action"], "id" => $player->id())); ?>"><?php TextManager::tr($action["name"]); ?></a>
				<?php endforeach; ?>
			</td>
			<td>
				<form action = "<?php $this->url(array("action" => "ch_pass", "id" => $player->id())); ?>" method = "POST">
					<?php TextManager::tr("NEW_PASSWORD"); ?><input type = "password" name = "new_pass" />
					<br />
					<?php TextManager::tr("NEW_REPEAT_PASSWORD"); ?><input type = "password" name = "new_pass_repeat" />
					<br />
					<input type = "submit" value = "<?php TextManager::tr("CHANGE_PASS") ?>" />
				</form>
			</td>
		</tr>
		<?php endforeach; ?>
	</table>
	<?php foreach ($errors as $error): ?>
		<div class = "error">*<?php TextManager::tr($error); ?></div>
	<?php endforeach; ?>
	<?php if ($this->model()->hasMsg()): ?>
		<div class = "action_msg">
			<?php
				$msg = $this->model()->msg();
				if ($msg["type"] == "for_player")								
					printf(TextManager::tr($msg["text"], false), $msg["player"]->name());
			?>
		</div>
	<?php endif; ?>
	<?php
	}
	
	private function confirm()
	{
		$playerList = $this->model()->playerList();
		$id = Input::request("id");
		
	?>
	<div>
	<?php printf(TextManager::tr("ASK_TO_REMOVE_PLAYER", false), $playerList[$id]->name()); ?>
	<br /><a href = "<?php $this->url(array("action" => "delete", "id" => $id)); ?>"><?php TextManager::tr("YES"); ?></a>
	<br /><a href = "<?php $this->url(); ?>"><?php TextManager::tr("NO"); ?></a>
	</div>
	<?php
	}
	
}

?>